/**
 *******************************************************************************
 * \brief   CAS EBX Project Work "Kaffeemaschine"
 *
 * \file    statistic.c
 * \version 1.0
 * \date    30.06.2010
 * \author  Baehler Thomas, Liechti Beat, Sauter Marc
 * \author  Vuksic Valentina, Zbinden Rene
 *
 * \remark  Last Modifications:
 * \remark  V1.0, 30.06.2010   Initial release
 *******************************************************************************
 **/

/*
 *******************************************************************************
 * Definitions
 *******************************************************************************
 */


/*
 *******************************************************************************
 * Imports
 *******************************************************************************
 */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>

#include "coffee2.h"
#include "channel.h"
#include "pool.h"
#include "device.h"


/*
 *******************************************************************************
 * Local Variables
 *******************************************************************************
 */

static tChannel /*c1,*/ *c9;
static tPool *p1;
static tDevice *d1;

/*
 *******************************************************************************
 * Local Function Declaration
 *******************************************************************************
 */
static void sigtermHandler(int signal);


/**
 *******************************************************************************
 * sigtermHandler
 *
 * \brief handle SIGTERM signal, close all pools and channels and exit
 *
 *******************************************************************************
 **/
static void sigtermHandler(int signal) {
	syslog(LOG_INFO, "SIGTERM reveiced, stopping ...");
	/* close channels and pools */
	//closeChannel(c1);
	closeDevice(d1);
	closeChannel(c9);
	closePool(p1);
	/* close syslog */
	closelog();
	/* close and remove the runfile */
	exit(EXIT_SUCCESS);
}


/**
 *******************************************************************************
 * mainStatistic
 *
 * \brief Activity: "Statistik Ausgabe"
 *
 * \return pid
 *
 *******************************************************************************
 **/
pid_t mainStatistic(){
	pid_t pid;
	sigset_t sigmask;
	struct sigaction sigtermAction, defaultAction;
	int statistic, c9Message;

	pid = fork();
	if (pid == 0) {
		/* syslog */
		closelog();
		openlog("coffee-statistic", LOG_PID, LOG_DAEMON);
		/* block all signals other than SIGTERM */
		sigemptyset(&sigmask);
		sigaddset(&sigmask, SIGTERM);
		sigtermAction.sa_handler = sigtermHandler;
		sigtermAction.sa_mask = sigmask;
		if (sigaction(SIGTERM, &sigtermAction, &defaultAction) == -1){
			syslog(LOG_ERR, "unable to setup signal handling!");
		}
		//c1 = openChannelWrite("C1", sizeof(int)); /* write to "Device" */
		d1 = openDeviceWrite("/dev/c2service");
		c9 = openChannelRead("C9", sizeof(int));  /* read from
													"User Interface" */
		p1 = openPool("P1", sizeof(statistic));   /* "Statistikdaten" */

		while (1) {
			if (readChannel(c9, &c9Message) > 0 && c9Message == STATISTIK) {
				if (readFromPool(p1, &statistic) > 0) {
					syslog(LOG_INFO, "writing statistics");
					//writeChannel(c1, &statistic);
					writeDevice(d1, statistic);
				}
			}
			usleep(500);
		}
	}
	return pid;
}

